package com.netease.nimlib.fts.test;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.app.Activity;

public class MainActivity extends Activity {

	private String luceneIndexDirPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.luceneIndexDirPath = getApplicationInfo().dataDir + "/lucene6/";

		/**
		 * 打开数据库
		 */
		findViewById(R.id.open).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.open(v.getContext().getApplicationInfo().dataDir + "/");
			}
		});

		/**
		 * 关闭数据库
		 */
		findViewById(R.id.close).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.close();
			}
		});

		/**
		 * 插入单条数据
		 */
		findViewById(R.id.insert).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.insert(((EditText) findViewById(R.id.content_text)).getText().toString());
			}
		});

		/**
		 * 批量插入数据
		 */
		findViewById(R.id.bulk_insert).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.bulkInsert(((EditText) findViewById(R.id.content_text)).getText().toString());
			}
		});

		/**
		 * 多线程批量插入数据
		 */
		findViewById(R.id.multi_bulk_insert).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.multiBulkInsert(((EditText) findViewById(R.id.content_text)).getText().toString());
			}
		});

		/**
		 * 查询结果（单条循环）
		 */
		findViewById(R.id.query_hit).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.query(((EditText) findViewById(R.id.query_text)).getText().toString());
			}
		});

		/**
		 * 查询结果（一次N条）
		 */
		findViewById(R.id.queryN_hit).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.queryN(((EditText) findViewById(R.id.query_text)).getText().toString());
			}
		});

		/**
		 * 分词测试
		 */
		findViewById(R.id.query_token_native).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.queryTokenNative(((EditText) findViewById(R.id.query_text)).getText().toString());
			}
		});

		findViewById(R.id.delete).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.delete();
			}
		});

		findViewById(R.id.delete2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.delete2();
			}
		});

		findViewById(R.id.delete3).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.delete3();
			}
		});

		/**
		 * 测试数据库版本号
		 */
		findViewById(R.id.test_version).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.testVersion();
			}
		});

		/**
		 * JNI测试
		 */
		findViewById(R.id.test).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test.test();
			}
		});

		/**
		 * Lucene insert
		 */
		findViewById(R.id.lucene_insert).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				try {
					Thread th = new Thread(new Runnable() {
						@Override
						public void run() {
							try {
								String content = ((EditText) findViewById(R.id.content_text)).getText().toString();
								TestLucene.insert(v.getContext(), luceneIndexDirPath, content);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					th.start();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

		/**
		 * Lucene query
		 */
		findViewById(R.id.lucene_query).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Thread th = new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							String content = ((EditText) findViewById(R.id.query_text)).getText().toString();
							TestLucene.query(luceneIndexDirPath, content);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				th.start();
			}
		});

		/**
		 * Lucene query page
		 */
		findViewById(R.id.lucene_query_page).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TestLucene.queryPage(luceneIndexDirPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		/**
		 * Lucene query next page
		 */
		findViewById(R.id.lucene_query_next_page).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TestLucene.queryNextPage(luceneIndexDirPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		/**
		 * Lucene query all
		 */
		findViewById(R.id.lucene_query_all).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Thread th = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							TestLucene.queryAll(luceneIndexDirPath);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				th.start();
			}
		});

		/**
		 * Lucene delete session
		 */
		findViewById(R.id.lucene_delete_session).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TestLucene.deleteSession(v.getContext(), luceneIndexDirPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		/**
		 * Lucene delete all
		 */
		findViewById(R.id.lucene_delete_all).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TestLucene.deleteAll(v.getContext(), luceneIndexDirPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		/**
		 * Lucene concurrency
		 */
		findViewById(R.id.lucene_concurrency).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					TestLucene.concurrency(v.getContext(), luceneIndexDirPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
